<?php

class m_pmi extends CI_Model {

	function stock($id_udd, $limit, $page)
	{
		$this->db->where('id_udd', $id_udd); 
		$this->db->order_by('datetime_created', "desc");
		$this->db->limit($limit, $page*$limit); 
		return $this->db->get('stock')->result_array();
	}
	
	function udd()
	{
		return $this->db->get('udd')->result_array();
	}
	
	function stocterbarubyudd($id_udd)
	{
		$this->db->select('stock.id_stock, udd.id_udd, udd.udd, stock.a_prc, stock.a_wb, stock.a_tc, stock.b_prc, stock.b_wb, stock.b_tc, stock.ab_prc, stock.ab_wb, stock.ab_tc,  stock.o_prc, stock.o_wb, stock.o_tc, stock.datetime_created');
		$this->db->join('udd', 'stock.id_udd = udd.id_udd', 'right');
		$this->db->where('udd.id_udd', $id_udd);
		$this->db->order_by('stock.id_stock', 'desc');
		$this->db->limit(1);
		$query= $this->db->get('stock');
		$result= $query->result_array();
		return $result[0];
	}
	
	function tambahudd($data='')
	{
		$result = $this->db->insert('udd', $data); 
		return $result;
	}
	function tambahstock($data='')
	{
		$result = $this->db->insert('stock', $data); 
		return $result;
	}
	
	function hapusudd($id='')
	{
		$result = $this->db->delete('udd', array('id_udd' => $id)); ; 
		return $result;
	}
	
	function hapusstock($id='')
	{
		$result = $this->db->delete('stock', array('id_stock' => $id)); ; 
		return $result;
	}
	
	function uddbyid($id='')
	{
		$this->db->where('id_udd', $id); 
		$result = $this->db->get('udd')->result_array();
		return $result[0];
	}
	
	function editudd($data='')
	{
		$this->db->where('id_udd', $data['id_udd']); 
		$result = $this->db->update('udd', $data); 
		return $result;
	}
	
	function login($username,$password)
	{
		$this->db->where('username', $username); 
		$this->db->where('password', $password);
		$query = $this->db->get('user');
	
		if($query->num_rows > 0)
        {
            // If there is a user, then create session data
            $result=$query->result_array();
            $data = array(
                    'username' => $result[0]['username'],
                    'validated' => true
                    );
            $this->session->set_userdata($data);
			return $result[0]['id_user'];
        }else
        {
        	return 0;
			
        }
	}

}
?>